package com.lwj.liwanjia.view.fragment.shouye;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.PromotionAdapter;
import com.lwj.liwanjia.base.BaseFragment;
import com.lwj.liwanjia.bean.AddGwcBean;
import com.lwj.liwanjia.bean.PromotionBean;
import com.lwj.liwanjia.impl.AddGwcView;
import com.lwj.liwanjia.impl.PromotionView;
import com.lwj.liwanjia.presenter.AddGwcPresenter;
import com.lwj.liwanjia.presenter.PromotionPresenter;
import com.lwj.liwanjia.utils.FragNavController;
import com.lwj.liwanjia.utils.NetWorkUtil;
import com.lwj.liwanjia.utils.RxBus;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.cuxiao.PromotionMoreActivity;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 促销商品
 */
public class PromotionFragment extends BaseFragment implements PromotionView {

    @BindView(R.id.rlv_shangpin)
    SuperRecyclerView rlvShangpin;
    @BindView(R.id.progress)
    ProgressBar progress;

    private List<PromotionBean.DataBean> list = new ArrayList<>();
    private PromotionAdapter promotionAdapter;
    private PromotionPresenter presenter;
    private View moreView;
    private LinearLayout moreLayout;

    @Override
    protected void initView(View view) {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        rlvShangpin.setLayoutManager(manager);
        rlvShangpin.setRefreshEnabled(false);
        rlvShangpin.setLoadMoreEnabled(false);
        rlvShangpin.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //不是第一个的格子都设一个左边和底部的间距
                outRect.left = (int) getResources().getDimension(R.dimen.x2);
                outRect.bottom = (int) getResources().getDimension(R.dimen.x2);
            }
        });
        promotionAdapter = new PromotionAdapter(getActivity(), list);
        addFootView();
        rlvShangpin.setAdapter(promotionAdapter);
        presenter = new PromotionPresenter(this);
        clickListener();

    }

    /**
     * 添加尾部局
     */
    private void addFootView() {
        LayoutInflater inflate = LayoutInflater.from(getActivity());
        moreView = inflate.inflate(R.layout.more_item_layout, null);
        moreLayout = (LinearLayout) moreView.findViewById(R.id.more_shangpin);
        //点击查看更多
        moreLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PromotionMoreActivity.class);
                startActivity(intent);
            }
        });
        promotionAdapter.addFooterView(moreView);
    }

    private void clickListener() {
        //详情
        promotionAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(getActivity(), ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getPromotionData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            initData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_promotion;
    }

    @Override
    public void showProgress() {
//        progress.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
//        progress.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(getActivity(), "数据加载失败！", Toast.LENGTH_SHORT).show();
    }

    /**
     * 获取商品数据
     *
     * @param promotionBean
     */
    @Override
    public void getPromotionData(PromotionBean promotionBean) {
        if (promotionBean.getState() == 1) {
            list.clear();
            list.addAll(promotionBean.getData());
            if (list.size() > 0) {
                moreLayout.setVisibility(View.VISIBLE);
                promotionAdapter.notifyDataSetChanged();
            } else {
                moreLayout.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
